<template>
  <div class="login">
    <h1 class="heading">用户登录</h1>
    <el-form :model="form" :rules="rules" ref="form" label-width="50px" autocomplete="off">
      <el-form-item label="账户" prop="username">
        <el-input v-model.trim="form.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model.trim="form.password" show-password />
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="submit">登录</el-button>
        <el-button type="danger">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {},
      rules: {
        username: { required: true, message: '请输入账户！' },
        password: { required: true, message: '请输入密码！' }
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 收集到了用户登录信息，然后post提交给后端
          this.$http.post('admin/login', JSON.stringify(this.form)).then(res => {
            if (res.err > 0) {
              this.$message.error(res.desc)
            } else {
              localStorage.setItem('Gx-Token', res.token)
              this.$router.replace('/control')
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login{
  position: fixed;
  width: 320px;
  border-radius: 4px;
  box-shadow: 0 0 6px rgba(0,0,0,.15);
  padding: 20px;
  left: 50%;
  top: 20vh;
  margin-left: -150px;
  background-color: white;
  .heading{
    text-align: center;
    font-size: 24px;
    padding-top: 10px;
    padding-bottom: 20px;
  }
  .btns{
    padding-left: 20px;
  }
}
</style>
